//
//  GrowRecordDetailCell.m
//  PigMarket
//
//  Created by mac on 2019/7/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "GrowRecordDetailCell.h"
#import "GrowRecordDetailCollectionView.h"

@interface GrowRecordDetailCell ()
/**
 leftTitle
 */
@property (nonatomic , strong) UILabel *leftTitleLab;
/**
 rightDetailLab
 */
@property (nonatomic , strong) UILabel *rightDetailLab;
/**
 deteLab
 */
@property (nonatomic , strong) YYLabel *dateLab;
/**
 section0 图片的collection视图
 */
@property (nonatomic , strong) GrowRecordDetailCollectionView *imgCollectionView;
@end
@implementation GrowRecordDetailCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    GrowRecordDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[GrowRecordDetailCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.leftTitleLab];
    [self.contentView addSubview:self.rightDetailLab];
    [self.contentView addSubview:self.imgCollectionView];
    [self.contentView addSubview:self.dateLab];
}

- (void)initLayout {
    // 布局子控件
    [self.imgCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    
    [self.leftTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.rightDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(20);
    }];
}

- (void)configCellWithInfoDic:(NSDictionary *)infoDic index:(NSIndexPath *)index {
    if (index.section == 0) {
        self.imgCollectionView.hidden = NO;
        self.leftTitleLab.hidden = YES;
        self.rightDetailLab.hidden = YES;
        self.imgCollectionView.dataArr = infoDic[@"detail"];
    } else {
        self.imgCollectionView.hidden = YES;
        self.leftTitleLab.hidden = NO;
        self.rightDetailLab.hidden = NO;
        self.leftTitleLab.text = infoDic[@"title"];
        self.rightDetailLab.text = infoDic[@"detail"];
    }
    if (index.section == 4 && index.row == 2) {
        self.rightDetailLab.hidden = YES;
        self.dateLab.hidden = NO;
        NSString *detailStr = infoDic[@"detail"];
        NSString *limitDayStr = infoDic[@"limitday"];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:detailStr];
        text.yy_color = [UIColor colorWithHexString:@"#A1A1A1"];
        text.yy_font = [UIFont systemFontOfSize:12];
        [text yy_setColor:[UIColor colorWithHexString:@"#F25C62"] range:NSMakeRange(detailStr.length-limitDayStr.length-1, limitDayStr.length)];
        [text yy_setFont:[UIFont systemFontOfSize:22] range:NSMakeRange(detailStr.length-limitDayStr.length-1, limitDayStr.length)];
        self.dateLab.attributedText = text;
    } else {
        self.rightDetailLab.hidden = NO;
        self.dateLab.hidden = YES;
    }
}
#pragma mark - getter and setter

- (UILabel *)leftTitleLab {
    if (!_leftTitleLab) {
        _leftTitleLab = [[UILabel alloc] init];
        _leftTitleLab.textColor = [UIColor colorWithHexString:@"#262626"];
        _leftTitleLab.font = [UIFont systemFontOfSize:16];
    }
    return _leftTitleLab;
}
- (UILabel *)rightDetailLab {
    if (!_rightDetailLab) {
        _rightDetailLab = [[UILabel alloc] init];
        _rightDetailLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _rightDetailLab.font = [UIFont systemFontOfSize:16];
        _rightDetailLab.textAlignment = NSTextAlignmentRight;
    }
    return _rightDetailLab;
}
- (YYLabel *)dateLab{
    if (!_dateLab) {
        _dateLab = [[YYLabel alloc] init];
        _dateLab.hidden = YES;
    }
    return _dateLab;
}
- (GrowRecordDetailCollectionView *)imgCollectionView {
    if (!_imgCollectionView) {
        _imgCollectionView = [[GrowRecordDetailCollectionView alloc] init];
    }
    return _imgCollectionView;
}
@end
